package com.feihong.txgw.shopcar.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.feihong.txgw.R;
import com.feihong.txgw.model.greendao.ShopCarGoodsModel;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;


/**
 * @name txgw_app
 * @class name：com.feihong.txgw.shopcar.adapter
 * @class describe:
 * @author wangziheng
 * @time 2017/8/5 19:32
 * @change
 * @chang time
 * @class describe
 */
public class ShopCarOrderGoodsAdapter extends BaseAdapter {
    public Context mContext;
    private List<ShopCarGoodsModel> mGoods;
    public ShopCarOrderGoodsAdapter(Context mcontext){
        mContext = mcontext;
        mGoods = new ArrayList<>();
    }

    public void setData(List<ShopCarGoodsModel> data){
        mGoods = data;
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return mGoods.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
        MyViewHolder viewHolder;
        if(convertView == null){
            viewHolder = new MyViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.shopcar_createorder_recycler_item_layout,viewGroup,false);
            viewHolder.initView(convertView);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (MyViewHolder) convertView.getTag();
        }
        viewHolder.initData(i);
        return convertView;
    }

    class MyViewHolder{
        public TextView mGoodsName;
        public TextView mGoodsDesc;
        public TextView mGoodsPriceFirst,mGoodsPriceTail;
        public TextView mGoodsCount;
        public ImageView mGoodsImg;
        /*小计模块*/
        public TextView mPrice_GoodsCount;
        public TextView mPrice_GoodsPrice;

        void initView(View convertView){
            mGoodsName = (TextView) convertView.findViewById(R.id.shopcar_createOrder_Goods_name);
            mGoodsDesc = (TextView) convertView.findViewById(R.id.shopcar_createOrder_Goods_desc);
            mGoodsImg = (ImageView) convertView.findViewById(R.id.shopcar_createOrder_Goods_img);
            mGoodsPriceFirst = (TextView) convertView.findViewById(R.id.shopcar_createOrder_Goods_price_first);
            mGoodsPriceTail = (TextView) convertView.findViewById(R.id.shopcar_createOrder_Goods_price_tail);
            mGoodsCount = (TextView) convertView.findViewById(R.id.shopcar_createOrder_Goods_count);
            /*小计*/
            mPrice_GoodsCount = (TextView) convertView.findViewById(R.id.shopcar_createOrder_Goods_price_count);
            mPrice_GoodsPrice = (TextView) convertView.findViewById(R.id.shopcar_createOrder_Goods_price_allCount);
        }

        void initData(int position){
            mGoodsName.setText(mGoods.get(position).getGoodsName());

            StringBuffer stringBuffer = new StringBuffer();
            try {
                JSONArray jsonArray = new JSONArray(mGoods.get(position).getGoodsSpec());
                for(int i = 0 ; i < jsonArray.length(); i ++){
                    JSONObject jsonObject = jsonArray.optJSONObject(i);
                    stringBuffer.append(jsonObject.optString("name")+":"+jsonObject.optString("value")+"  ");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            mGoodsDesc.setText(stringBuffer.toString() );
            String sp = String.valueOf(mGoods.get(position).getGoodsPrice());
            String sigle_price[] = sp.split("\\.");
            if(TextUtils.isEmpty(sigle_price[1]) || "00".equals(sigle_price[1]) || "0".equals(sigle_price[1])){
                mGoodsPriceFirst.setText("¥ "+sigle_price[0]);
            }else {
                mGoodsPriceFirst.setText("¥ " + sp);
            }
            Glide.with(mContext).load(mGoods.get(position).getGoodsPic()).into(mGoodsImg);

            BigDecimal oneA1 = new BigDecimal(Double.toString(mGoods.get(position).getGoodsPrice()));
            BigDecimal price = new BigDecimal(Double.toString(mGoods.get(position).getGoodsNum()));
            BigDecimal onePrice = new BigDecimal("1");
            DecimalFormat oneDF = new DecimalFormat("######0.00");
            BigDecimal result = oneA1.multiply(price);
            double onePriceStr  = result.divide(onePrice,2,BigDecimal.ROUND_HALF_UP).doubleValue();
            String allPrice = oneDF.format(onePriceStr);
            String[] strings = allPrice.split("\\.");
            if(TextUtils.isEmpty(strings[1]) || "00".equals(strings[1]) || "0".equals(strings[1])){
                mPrice_GoodsPrice.setText("¥ " +strings[0]);
            }else {
                mPrice_GoodsPrice.setText("¥ " +allPrice);
            }
            //mGoodsPriceTail.setText(strings[1]);
//            mGoodsPriceTail.setText("");
            mGoodsCount.setText("x "+mGoods.get(position).getGoodsNum());
//            int money=Integer.parseInt(strings[0])* mGoods.get(position).getGoogdsnum();
            mPrice_GoodsCount.setText("共"+mGoods.get(position).getGoodsNum()+"件商品  小计：");
        }
    }
}
